package vnreal.gui.menu;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import vnreal.ZNEW.XmlTest;
import vnreal.ZNEW.GraphTest.WorkOnPaths;
import vnreal.gui.GUI;
import vnreal.map.Map;

@SuppressWarnings("serial")
public class MapMenu extends JMenu implements ActionListener {
	public static JFrame frame;

	public MapMenu() {
		super("Map");
		setMnemonic(KeyEvent.VK_A);
		JMenuItem mi;

		mi = new JMenuItem("Show map");
		mi.setActionCommand("showMap");
		mi.addActionListener(this);
		add(mi);
		addSeparator();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals("showMap")) {
			XmlTest.deserialize();
			System.out.println("Open                   " + Map.numberOfElements);
			if(XmlTest.test.doc != null) {
				
				//XmlTest.serialize();
			}
			frame = new JFrame("Map");
			frame.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					GUI.singleton.setEnabled(true);
					WorkOnPaths.doMappingOnScenario();
					System.out.println(XmlTest.xstream.toXML(XmlTest.scenario));
					//XmlTest.deserialize();
					System.out.println("Close przed                   " + Map.numberOfElements);
					XmlTest.serialize();
					System.out.println("Close po                   " + Map.numberOfElements);
				}
			});
			Map.showMap();
			frame.getContentPane().add(Map.mapKit);
			frame.setSize(800, 600);
			GUI.singleton.setEnabled(false);
			// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			frame.setVisible(true);
		}
	}

}
